package lmscript.postscript.syntax;

import lmscript.postscript.syntax.stack.OperandStack;

public abstract class PSOperator extends PSSimple {

	protected Class[] operandTypes;

	PSOperator(String name) {
		super(name, EXECUTABLE);
	}

	public boolean checkOperandType(OperandStack oStack) {
		return false;
	}

	public String getTypeString() {
		return super.getTypeString() + ":operator";
	}

	public Object clone() {
		return super.clone();
	}

	protected boolean check(OperandStack os) {
		if (this.operandTypes == null || this.operandTypes.length <= 0) {
			return false;
		}

		if (this.operandTypes.length > os.count()) {
			throwError(os, new StackUnderflow());
			return false;
		}

		for (int i = 0; i < this.operandTypes.length; i++) {
			Class clz = this.operandTypes[i];
			if (!clz.isInstance(os.peek(i))) {
				throwError(os, new TypeCheck());
				return false;
			}
		}
		return true;
	}
}
